﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace CheccCoverplateCulvertAssemble.Operation.section
{
    internal class DgvSubsectionData
    {
        /// <summary>
        /// 将板长分段数据填入到DGV
        /// </summary>
        /// <param name="lengthOfPanelSubsection">板长分段数据</param>
        /// <param name="SectionSubsection">区间分段数据</param>
        public static void SetSubsectionDgv(List<double> lengthOfPanelSubsection, List<double> SectionSubsection,DataGridView dataGridView)
        {
            dataGridView.Rows.Clear();  
            dataGridView.Rows.Add();
            foreach (double value in lengthOfPanelSubsection) { dataGridView.Rows.Add();}
            dataGridView.Rows.Add();
            double sumValue = 0;
            int SectionSubsectionIndex = 0;
            dataGridView[0, 0].Value ="0";
            dataGridView[1, 0].Value = "左洞口";
            dataGridView[2, 0].Value = true;
            for (int i = 0; i < lengthOfPanelSubsection.Count; i++)
            {
                dataGridView[0, i+1].Value = (i+1).ToString();
                dataGridView[1, i+1].Value = (lengthOfPanelSubsection[i] * 100).ToString();
                sumValue += lengthOfPanelSubsection[i] * 100;
                if(SectionSubsectionIndex>= SectionSubsection.Count)
                    break;


                if (sumValue.ToString() == (SectionSubsection[SectionSubsectionIndex] * 100).ToString())
                {
                    dataGridView[2, i + 1].Value = true;
                    sumValue = 0;
                   
                    SectionSubsectionIndex++;
                }
                else
                    dataGridView[2, i + 1].Value = false;
            }
            dataGridView[0, dataGridView.Rows.Count - 1].Value = (dataGridView.Rows.Count - 1).ToString();
            dataGridView[1, dataGridView.Rows.Count - 1].Value = "右洞口";
            dataGridView[2, dataGridView.Rows.Count - 1].Value = false;

            //首尾不可编辑
            dataGridView[2, 0].ReadOnly = true;
            dataGridView[2, dataGridView.Rows.Count - 1].ReadOnly = true;

        }

        /// <summary>
        /// 根据板长分段求涵身分段
        /// </summary>
        /// <param name="dataGridView">板长分段控件</param>
        public static List<double> ReadDgvConvertSectionSubsection(DataGridView dataGridView)
        {
            List<double> newSectionSubsection = new List<double>();
            double sum = 0;
            for (int i = 1; i < dataGridView.Rows.Count - 1; i++)
            {
                sum += Convert.ToDouble(dataGridView[1, i].Value);
                DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)dataGridView[2, i];
                if (Convert.ToBoolean(cell.Value) == true)
                {
                    newSectionSubsection.Add(sum);
                    sum = 0;
                }
            }
            return newSectionSubsection;
        }

        /// <summary>
        /// 根据板长分段求涵身分段
        /// </summary>
        /// <param name="dataGridView">板长分段控件</param>
        public static List<double> ReadDgvConvertSlabSubsection(DataGridView dataGridView)
        {
            List<double> newSectionSubsection = new List<double>();
            for (int i = 1; i < dataGridView.Rows.Count - 1; i++)
            {
                newSectionSubsection.Add(Convert.ToDouble(dataGridView[1, i].Value));
            }
            return newSectionSubsection;
        }
    }
}
